from rest_framework import serializers
from rest_framework.relations import StringRelatedField

from day02_books import models

class BookTypeSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.BookType
        fields = '__all__'

class BookSerializer(serializers.ModelSerializer):
    type_name = StringRelatedField(source="book_type")
    str_time = serializers.SerializerMethodField()
    class Meta:
        model = models.BookInfo
        fields = '__all__'
    def get_str_time(self,attr):
        return attr.create_time.strftime('%Y-%m-%d %H:%M:%S')